package com.varian.task.util;

import com.varian.rabbit.util.RabbitUtil;
import com.varian.security.util.SecurityUtil;
import com.varian.task.model.TaskCreateModel;
import com.varian.task.model.TaskUpdateModel;
import com.varian.tool.model.ExecuteType;
import com.varian.tool.model.TaskStatus;
import lombok.experimental.UtilityClass;

import java.time.LocalDateTime;

import static com.varian.task.constant.TaskConstant.*;

/**
 * @author ben
 * @since 2024/11/25
 */
@UtilityClass
public class TaskUtil {


    public void createTask(String taskCode, Object taskParam) {
        createTask(taskCode, taskCode, taskParam);
    }

    public void createTask(String taskCode, String taskName, Object taskParam) {
        createTask(taskCode, taskName, ExecuteType.IMMEDIATE, null, 0, taskParam);
    }

    public void createTask(String taskCode, String taskName, Integer executeBlockStrategy, Object taskParam) {
        createTask(taskCode, taskName, ExecuteType.IMMEDIATE, null, executeBlockStrategy, taskParam);
    }

    public void createTask(String taskCode, String taskName, ExecuteType executeType, LocalDateTime scheduleTime, Integer executeBlockStrategy, Object taskParam) {
        TaskCreateModel job = new TaskCreateModel();
        job.setTaskCode(taskCode);
        job.setTaskName(taskName);
        job.setTaskParam(taskParam);
        job.setExecuteType(executeType);
        job.setScheduleTime(scheduleTime);
        job.setExecuteBlockStrategy(executeBlockStrategy);
        job.setCreateId(SecurityUtil.getUserIdOnSafe());
        RabbitUtil.send(EXCHANGE_TASK, RK_TASK_CREATE, job);
    }

    public void updateTaskStatus(Long taskId, TaskStatus taskStatus, LocalDateTime executeTime, String errorMessage) {
        TaskUpdateModel task = new TaskUpdateModel()
                .setTaskId(taskId)
                .setTaskStatus(taskStatus)
                .setExecuteTime(executeTime)
                .setErrorMessage(errorMessage);
        RabbitUtil.send(EXCHANGE_TASK, RK_TASK_UPDATE, task);
    }
}
